package com.yun.algorithmproblem.leetcode;

public class Leetcode1963 {

    public static void main(String[] args) {
        Leetcode1963 obj = new Leetcode1963();
        System.out.println(obj.minSwaps("][]["));
    }

    public int minSwaps(String s) {
        //[左边的[数量需要大于等于[左边的]数量,否则将最右边的]与最左边的[交换
        int ans = 0;
        int left = 0;
        char[] chars = s.toCharArray();
        int p = 0, q = chars.length - 1;
        while (p < q) {
            if (chars[p] == '[') {
                left++;
            } else {
                left--;
            }
            if (left < 0) {
                while (chars[q] != '[') {
                    q--;
                }
                chars[q] = ']';
                q--;
                left += 2;
                ans++;
            }
            p++;
        }
        return ans;
    }
}
